package org.apogee.mil.client.main.pages.admin.forms;

import java.util.ArrayList;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.main.pages.admin.forms.widgets.TraineeWidget;
import org.apogee.mil.client.main.pages.admin.forms.widgets.TrainerWidget;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.Constants.DisplayFormat;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class AdminTrainerForm extends LayoutContainer {

	private TrainerWidget trainer;
	private TraineeWidget trainee;
	private ContentPanel cp;
	private int currentTrainerId;
	private int sessionId;
	
	public AdminTrainerForm() {
		trainer = new TrainerWidget(this);
		trainee = new TraineeWidget();
		sessionId = Session.getSessionId();
		currentTrainerId = -1;
	}
	
	@Override
	public void onRender(Element parent, int index) {
		super.onRender(parent, index);
		
		cp = new ContentPanel();
		cp.setHeading("Add Trainee to Trainer");
		cp.setBorders(true);
		cp.setWidth(500);
		cp.setHeight(500);
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(trainer.asWidget());
		hp.add(trainee.asWidget());
		
		cp.add(hp);
		
		cp.setButtonAlign(HorizontalAlignment.CENTER);
		cp.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  

			@Override  
			public void componentSelected(ButtonEvent ce) {  
				trainer.getStore().rejectChanges();
				trainee.getStore().rejectChanges();

			}  
		})); 
		
		cp.addButton(new Button("Save", new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				
				if (currentTrainerId == -1) {
					MessageBox.info("Information", "No Trainer has been selected", null);
					return;
				}
				
				
				ArrayList<BaseModel> changes = new ArrayList<BaseModel>();
				ArrayList<Record> traineeChanges = (ArrayList<Record>) trainee.getStore().getModifiedRecords();
				
				for (Record r : traineeChanges) {
					BaseModel bm = new BaseModel();
					bm.set(DisplayFormat.TRAINER_CHANGES, DisplayFormat.TRAINER_CHANGES);
					
					for (String s : r.getPropertyNames()) {
						bm.set(s, r.get(s));
					}
					
					changes.add(bm);
					
					if (bm.get(DisplayFormat.DELETE) != null && bm.get(DisplayFormat.DELETE).toString().toLowerCase().equals("true")) {
						trainee.getStore().remove((BaseModel) r.getModel());
					}
					
				}
				
				
				ServerServiceFactory.getAdminService(sessionId).updateTrainer(currentTrainerId, changes, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if (result == false) {
							MessageBox.alert("Error", "Unable to update Trainers", null);
							trainee.getStore().rejectChanges();
							return;
						}
						
						Info.display("Success", "Trainers updated successfully");
						trainee.getStore().commitChanges();
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Unable to update Trainers\n" + caught.getMessage(), null);
						
					}
				});
				
			}
		}));
		
		add(cp);
		layout();
	}

	public void updateTrainees(int trainerId) {
		currentTrainerId = trainerId;
		trainee.update(trainerId);
		
	}

}
